package cn.cvs.servlet.storagerecord;

import cn.cvs.service.StorageRecordService;
import cn.cvs.util.SpringUtil;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet(name = "DelStorageServlet", urlPatterns = "/sys/storageRecord/del")
public class DelStorageServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet ( request, response );
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding ( "utf-8" );
        response.setContentType ( "text/html;charset=UTF-8" );
        StorageRecordService storageRecordService= (StorageRecordService) SpringUtil.getBean ( "stoageRecordService" );
        int id = Integer.parseInt ( request.getParameter ( "id" ) );
        System.out.println ( id );
        Map map=new HashMap ();
        if (storageRecordService.getStorageRecordById ( id )!=null) {
            int del = storageRecordService.del ( id );
            if (del>0){
                map.put ( "delResult","true" );
            }else {
                map.put ( "delResult","false" );
            }
        }else {
            map.put ( "delResult","notexist" );
        }
        PrintWriter out = response.getWriter ( );
        out.print ( JSON.toJSONString ( map ) );
        out.close ();
    }
}
